<template>
    <div>
        <head-nav></head-nav>
        <!-- 占位 -->
        <div class="zanwei-top"></div>
        <div class="insetbox" v-if="pageshow">
            <div class="rappay">
                <div class="tit">
                    <div class="tit">
                        <i class="iconfont icon-houtui"></i>
                        <span @click="goback">订单支付</span>
                    </div>
                </div>
                <!-- <div class="time">
                    <i></i>
                    <span>支付剩余时间 ：{{minute}}分钟{{second}}秒</span>
                </div> -->
                <div class="pay_box">
                    <h4>结算信息</h4>
                    <ul class="rap_intro">
                        <li class="dd_name">
                            <p>礼品名称：</p>
                            <p>{{jpdata.SPMC}}</p>
                        </li>
                        <li class="dd_name">
                            <p>礼品数量：</p>
                            <p>{{lpsl}}</p>
                        </li>
                        <li class="dd_name">
                            <p>礼品价格：</p>
                            <p>{{jpdata.XJ}}元</p>
                        </li>
                        <li>
                            <div>支付金额：</div>
                            <span class="dd_je">{{sumAll}}元</span>
                        </li>
                        <!--<li>
                            <div>钱包余额：</div>
                            <span>{{itemObj.qbye}}元</span>
                        </li>-->
                        <!--<li>
                            <div>支付金额：</div>
                            <span class="dd_je">{{total}}元</span>
                        </li>-->
                    </ul>
                    <h4>选择支付方式</h4>
                    <ul class="gopay">
                        <li>
                            <div class="choose">
                                <div class="ph toFlexMid">
                                    <img src="../../assets/img/weixin.png"/>
                                </div>
                                <div class="cont">
                                    <strong>微信支付</strong>
                                    <p>推荐安装微信5.0及以上版本的使用</p>
                                </div>
                            </div>
                            <div class="icon icon_select"></div>
                        </li>
                    </ul>
                    <div class="pay_btn" @click="toPay" :style="backgroundColor">确认支付</div>
                </div>
            </div>
        </div>
        <!--底部占位-->
        <div class="zanwei-bottom"></div>
        <!--防止点击-->
    </div>
</template>

<script>
    import {MessageBox, Toast} from 'mint-ui';
    import api from '@/api/api'
    import pay from '@/api/pay'

    export default {
        data() {
            return {
                lpid: '',
                pageshow: true,
                tid: null,
                minute: '15',
                second: '00',
                time: 100,
                jpdata: {
                },
                lpsl: 1
            }
        },
        methods: {
            // 支付
            toPay() {
                clearInterval(this.tid);
                var that = this;
                var id = this.lpid;
                var lpsl = this.lpsl;
                MessageBox.confirm('确定支付吗?', '温馨提示').then(action => {
                    if (action === 'confirm') {
                        console.log('进行支付');
                        pay.createOrder(that,id, lpsl,(res_post)=>{
                            var data = res_post.data;
                            console.log(res_post,"创建订单");
                            if(data.code==0){
                                console.log(data.code,"状态")
                                var payData = JSON.parse(data.data);
                                var oid = payData.redisStr;
                                console.log(payData,"支付数据")
                                wx.chooseWXPay({
                                    timestamp: payData.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
                                    nonceStr: payData.nonceStr, // 支付签名随机串，不长于 32 位
                                    package: payData.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
                                    signType: payData.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
                                    paySign: payData.paySign, // 支付签名
                                    cancel: function (res) {
                                        //微信返回的状态 取消:res.errMsg == "chooseWXPay:cancel"
                                        api.lpBuyError(that, oid, cb);
                                        function cb(re) {
                                            let data = re.data;
                                            if(data.code == 0){
                                            }
                                        }
                                        Toast({
                                            message: '已取消支付',
                                            position: 'middle',
                                            duration: 2000
                                        });
                                        //退回上个页面
                                        history.back(-1);
                                    },
                                    error: function (res) {
                                        //退回上个页面
                                        api.lpBuyError(that, oid, cb);
                                        function cb(re) {
                                            let data = re.data;
                                            if(data.code == 0){
                                            }
                                        }
                                        Toast({
                                            message: '支付异常',
                                            position: 'middle',
                                            duration: 2000
                                        });
                                        history.back(-1);
                                    },
                                    success: function (res) {
                                        /*微信返回的状态 成功:res.errMsg == "chooseWXPay:ok"*/
                                        Toast({
                                            message: '支付成功',
                                            position: 'middle',
                                            duration: 2000
                                        });
                                        that.$router.push({path: '/rapright/'+ payData.oid});
                                        //路由到成功页面
                                    }
                                });
                            }else{
                                Toast({
                                    message: data.msg,
                                    duration: 2000
                                });
                            }
                        })
                    }
                });
            },
            // 返回
            goback() {
                history.back(-1);
            },
            // 倒计时
            timego() {
                var that = this;
                var time = this.time; //秒
                that.tid = setInterval(function () {
                    time--;
                    that.minute = that.addZero(Math.floor(time / 60));
                    that.second = that.addZero(Math.floor(time % 60));
                    if (time <= 0) {
                        clearInterval(that.tid);
                        that.pageshow = false;
                        Toast({
                            message: '订单超时',
                            position: 'middle',
                            duration: 1000
                        });
                        setTimeout(function () {
                            history.back(-1);
                        }, 1000);
                    }
                }, 1000);
            },
            // 补零
            addZero(n) {
                return n < 10 ? '0' + n : n;
            }
        },
        mounted() {
            this.lpid = this.$route.query.lpid;
            this.lpsl = this.$route.query.lpsl;
            let that = this;
            let userinfo = JSON.parse(sessionStorage.getItem('userinfo'));
            //查询产品信息
            api.scoreToGiftDetail(this, userinfo.vipcode,this.lpid,(res)=>{
                if(res.code==0){
                    let pldata = JSON.parse(res.data);
                    pldata.XJ = (pldata.XJ).toFixed(2);
                    that.jpdata = pldata;
                }else{

                }
                console.log(res,"支付-礼品")
            })
            //加载微信sdk
            api.getWxJsSdkConfig(this, (res) => {
                console.log(res, "返回结果");
                let data_res = JSON.parse(res.data.data);
                wx.config({
                    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                    appId: data_res.appId, // 必填，公众号的唯一标识
                    timestamp: data_res.timestamp, // 必填，生成签名的时间戳
                    nonceStr: data_res.nonceStr, // 必填，生成签名的随机串
                    signature: data_res.signature,// 必填，签名
                    jsApiList: ["chooseWXPay"] // 必填，需要使用的JS接口列表
                });
            })
            wx.ready(function () {
                console.log("执行了")
            })

        },
        computed: {
            total: function () {
                var je = this.itemObj.je;
                var je2 = this.itemObj.qbye;
                var je3 = parseFloat(je + je2).toFixed(2);
                return je3;
            },
            sumAll: function () {
            	if (this.jpdata.XJ != null) {
            		var dj = this.jpdata.XJ;
	            	var sl = this.lpsl;
	            	return (dj * sl).toFixed(2);
            	}
            }
        },
        // 路由变换清除定时器
        beforeRouteLeave(to, from, next) {
            clearInterval(this.tid);
            next();
        }
    }

</script>

<style lang="less">
    .insetbox {
        display: flex;
        justify-content: center;
        padding-top: 0.15rem;
        background-color: #fff;
    }

    .rappay {
        border: 0.01rem solid #eee;
        box-shadow: 0 0 0.05rem #eee;
        width: 7rem;
        .tit {
            padding-left: 0.15rem;
            display: flex;
            align-items: center;
            height: 0.88rem;
            // border-bottom: 0.01rem solid #f7f7f7;
            background-color: #f1f1f1;
            span {
                margin-left: 5px;
            }
        }
        .time {
            padding-left: 0.10rem;
            height: 0.72rem;
            display: flex;
            align-items: center;
            background-color: #fffbd8;
            border-right: 0.01rem solid #eee;
            color: #ffb22b;
            i {
                width: 0.2rem;
                height: 0.2rem;
                border-radius: 50%;
                background-color: #ffb22b;
                margin: 0 0.08rem;
            }
        }
        .pay_box {
            padding: 0.1rem;
        }
        h4 {
            padding-left: 0.2rem;
            line-height: 0.96rem;
            font-size: 0.3rem;
            font-weight: bold;
        }
        .rap_intro {
            background-color: #FFFFFF;
            border-radius: 0.04rem;
            box-shadow: 0 0 0.03rem #ccc;
            li {
                height: 0.92rem;
                display: flex;
                justify-content: space-between;
                align-items: center;
                border-bottom: 0.01rem solid #f7f7f7;
                padding: 0 0.1rem;
                padding-right: 0.2rem;
            }
            .dd_je {
                color: #ffb22b;
            }
        }
        .gopay {
            background-color: #FFFFFF;
            border-radius: 0.04rem;
            box-shadow: 0 0 0.03rem #aaa;
            li {
                display: flex;
                align-items: center;
                justify-content: space-between;
                padding: 0.16rem;
                .choose {
                    display: flex;
                    align-items: center;
                }
                .ph {
                    width: 1rem;
                    height: 0.7rem;
                    border-radius: 0.05rem;
                    background-color: #eff1f2;
                    box-shadow: 0 0 0.03rem #eee;

                    img {
                        width: 0.5rem;
                        height: 0.48rem;
                        background-color: #eff1f2;
                    }
                }
                .cont {
                    margin-left: 0.2rem;
                    strong {
                        font-size: 0.3rem;
                        line-height: 0.44rem;
                    }
                    p {
                        color: #ababab;
                        font-size: 0.24rem;
                        line-height: 0.4rem;
                    }
                }
                .icon {
                    width: 0.4rem;
                    height: 0.4rem;
                    border-radius: 50%;
                    border: 0.02rem solid #eee;
                    box-shadow: 0 0 0.05rem #eee;
                }
                .icon_select {
                    background: url(http://www.rtaksoft.com:3200/res/form/2c91a36a63d4cc100163dd9bc49e01ee/297eb6155f35043d015f3505adfd0004/9377620180609104554292.png) no-repeat;
                    background-size: cover;
                    border: none;
                }
            }
        }
        .pay_btn {
            width: 100%;
            height: 0.94rem;
            line-height: 0.94rem;
            background-color: #F05DC4;
            color: #fff;
            font-size: 0.3rem;
            text-align: center;
            margin-top: 0.2rem;
        }
    }

    .toFlexMid {
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .dd_name {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        display:flex;
        align-content: space-between;
        
    }
</style>
